package com.itwmk.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itwmk.entity.Category;
import com.itwmk.entity.DishDto;
import com.itwmk.entity.Setmeal;
import com.itwmk.entity.SetmealDto;
import com.itwmk.service.impl.CategoryService;
import com.itwmk.service.impl.SetmealServic;
import com.itwmk.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wmk
 */
@Slf4j
@RestController
@RequestMapping("setmeal")
@CacheEvict(value = "setmealCache",allEntries = true)
public class SetmealController {
    @Autowired
    private SetmealServic setmealServic;
    @Autowired
    private CategoryService categoryService;
    @PostMapping
    public R<String> addSetmeal(@RequestBody SetmealDto dto){
        setmealServic.addSetmealWiteDish(dto);
        return R.success("添加成功");
    }

    @GetMapping("page")
    public R<Page> querySetmeal(int page,int pageSize,String name){
        Page<Setmeal> setPage = new Page<>(page,pageSize);
        Page<SetmealDto> dtoPage = new Page<>(page,pageSize);

        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasLength(name),Setmeal::getName,name);
        wrapper.orderByAsc(Setmeal::getName);
        setmealServic.page(setPage,wrapper);
        BeanUtils.copyProperties(setPage,dtoPage,"records");

        List<Setmeal> records = setPage.getRecords();
        ArrayList<SetmealDto> list = new ArrayList<>();
        for (Setmeal s:records) {
            SetmealDto dto = new SetmealDto();
            Category category = categoryService.getById(s.getCategoryId());
            if (category!=null){
                String categoryName = category.getName();
                dto.setCategoryName(categoryName);
                BeanUtils.copyProperties(s,dto);
                list.add(dto);
            }
        }
        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }

    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> deleteSetmeal(@RequestParam List<Long> ids){
        log.info(ids.toString());
        setmealServic.removeWithDish(ids);

        return R.success("删除成功");
    }

    /**
     * 根据status和categoryId查询套餐
     * @param setmeal
     * @return
     */
    @GetMapping("list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<Setmeal>> setmealList(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealServic.list(wrapper);
        return R.success(list);
    }
}
